using System.Web;
using Shinkansen.Support;

namespace Shinkansen.Model
{
    public class CompressedStaticResource : IStaticResource
    {
        private readonly IStaticResource _resource;
        private readonly ResourceInformation _ri;

        /// <summary>
        /// Initializes a new instance of the <see cref="CompressedStaticResource"/> class.
        /// </summary>
        /// <param name="resource">The resource.</param>
        /// <param name="ri">The ri.</param>
        public CompressedStaticResource (IStaticResource resource, ResourceInformation ri)
        {
            _resource = resource;
            _ri = ri;
        }

        /// <summary>
        /// Gets the content.
        /// </summary>
        /// <returns></returns>
        public byte[] GetContent ()
        {
            byte[]  content = _resource.GetContent ();
            bool    clientSupportsHttpCompression = !string.IsNullOrEmpty (HttpContext.Current.Request.Headers["Accept-Encoding"]);

            if (clientSupportsHttpCompression && _ri.Compress)
            {
                HttpCompressor compressor = new HttpCompressor (_ri.CompressionMode);
                content = compressor.Compress (content);
            }

            return content;
        }
    }
}